
import wx

from CodeEditor import CodeEditor

class CodeTab(wx.Panel):
	"""Panel for the Code tab"""
	def __init__(self, parent):
		wx.Panel.__init__(self, parent, size=(1,1))

		self.title = None
		self.nb = None

		self.editor = CodeEditor(self)
		self.editor.RegisterLineNumberClickedEvent(self.AddRemoveBPOnLine)

		self.box = wx.BoxSizer(wx.VERTICAL)
		self.box.Add(self.editor, 1, wx.EXPAND)

		self.box.Fit(self)
		self.SetSizer(self.box)

		self.debugline = None
		self.breakpoints = set()

	def LoadSourceFromPath(self, path):
		self.filepath = path

		fp = open(path, 'rb')
		s = fp.read()
		fp.close()
		self.LoadSource(s)

	def LoadSource(self, source):
		self.editor.Clear()
		self.editor.SetValue(source)
	
	def Save(self):
		if self.filepath:
			self.editor.SaveFile(self.filepath)
			self.SetTabTitle(self.title)
		else:
			self.SaveAsNew()

	def GetFileFromDialog(self):
		filepath = None
		dlg = wx.FileDialog(self, "Open Lua file...", "",
				style=wx.SAVE, wildcard='*.lua')
		if dlg.ShowModal() == wx.ID_OK:
			filepath = dlg.GetPath()
		dlg.Destroy()
		return filepath

	def SaveAsNew(self):
		filepath = self.GetFileFromDialog()
		if filepath:
			if self.nb:
				self.nb.UpdateTabPath(self, filepath)
			self.editor.SaveFile(filepath)

	def GetTabIndex(self):
		if self.nb:
			for i in range(self.nb.GetPageCount()):
				if self == self.nb.GetPage(i):
					return i
		return -1

	def SetTabTitle(self, title):
		n = self.GetTabIndex()
		if n >= 0:
			self.nb.SetPageText(n, title)

	def SetTabInfo(self, title, nb):
		self.editor.RegisterModifiedEvent(self.OnCodeModified)
		self.title = title
		self.nb = nb

	def JumpToLine(self, line, highlight=False):
		self.editor.GotoLine(line)
		self.editor.SetFocus()
		if highlight:
			self.editor.SelectLine(line)


	def DebugOnLine(self, line):
		#self.JumpToLine(line, True)
		self.DebugNone()
		self.debugline = line

		self.editor.GotoLine(line)

		self.editor.MarkerAdd(line, self.editor.DEBUG_MARK)
		self.editor.MarkerAdd(line, self.editor.DEBUG_MARK2)

	def DebugNone(self):
		if self.debugline != None:
			self.editor.MarkerDelete(self.debugline, self.editor.DEBUG_MARK)
			self.editor.MarkerDelete(self.debugline, self.editor.DEBUG_MARK2)
			self.debugline = None
			# refresh to update line marker2: STC_MARK_BACKGROUND
			self.editor.Refresh()

	def AddBP(self):
		n = self.editor.GetCurrentLine()
		v = n + 1
		self.editor.MarkerAdd(n, self.editor.BP_MARK)
		self.breakpoints.add(v)

	def RemoveBP(self):
		n = self.editor.GetCurrentLine()
		v = n + 1
		if v in self.breakpoints:
			self.breakpoints.remove(v)
			self.editor.MarkerDelete(n, self.editor.BP_MARK)

	def AddRemoveBP(self):
		n = self.editor.GetCurrentLine()
		self.AddRemoveBPOnLine(n)
	
	def AddRemoveBPOnLine(self, n):
		v = n + 1
		if v in self.breakpoints:
			self.breakpoints.remove(v)
			self.editor.MarkerDelete(n, self.editor.BP_MARK)
		else:
			self.breakpoints.add(v)
			self.editor.MarkerAdd(n, self.editor.BP_MARK)

	def ClearBPs(self):
		for i in self.breakpoints:
			self.editor.MarkerDelete(i-1, self.editor.BP_MARK)
		self.breakpoints.clear()

	def OnCodeModified(self, event):
		if self.editor.IsModified():
			self.SetTabTitle(self.title + '*')

##################################################

def runTest(frame, nb, log):
	ed = p = CodeTab(nb)

	ed.editor.SetText(open('../src/toydebugger.lua').read())

	return p

if __name__ == '__main__':
	import sys,os
	import wxrun
	wxrun.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])

# vim: foldmethod=marker:
